home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_02_01
/
2n01041a
< prev
next >
Wrap
Text File
|
1990-06-24
|
11KB
|
431 lines
(* Panel Unit -- EgaVga Graphic Radio-Button Panel
Turbo Pascal 4 Unit
**************************************************
NOTE: All compilations should have the boolean
evaluation option set to "short-circuit"
**************************************************
Supports MicroSoft compatible Mouse Driver
June 26, 1990 -- Michael Kelly -- Version 1.01 *)
Unit Panel;
InterFace
Uses PanelDrv, Graph, Int16, Mouse;
Const
Gap = 25; { pixel space between buttons }
MaxButton = 10; { maximum number of buttons on panel }
Type
ButtonState = (Up, Down);
ButtonTag = String[80]; { option string }
Button = Record { button and associated option string }
State : ButtonState;
UpperLeftX,
UpperLeftY,
TagColor : Integer;
Tag : ButtonTag;
End;
Buttons = Array[1..MaxButton] of Button;
ButtonDescriptor = Record { attributes common to 3D buttons }
RadioButton : Buttons;
SideLength,
BorderWidth,
HighLightEdge,
ShadowEdge,
Normal,
TagHighLight,
TagNormal : Integer;
End;
PanelDescriptor = Record { defines a radio-button panel }
UpperLeftX,
UpperLeftY,
LowerRightX,
LowerRightY,
NumButtons, CurrentButton : Integer;
ButtonStyle : ButtonDescriptor;
End;
(*
* draws the panel defined by the PanelDescriptor parameter
*
* returns:
* True on success
*
* False on error
*)
Function DrawPanel(Var ButtonPanel : PanelDescriptor) : Boolean;
(*
* polling function updates button panel appearence to reflect
* keyboard and mouse input, and determines if a button has been
* pressed
*
* note: does not wait for input
*
* returns:
* 0 if no button pressed
*
* user selection in range 1 to MaxButton if button pressed
*)
Function ButtonPress(Var ButtonPanel : PanelDescriptor) : Integer;
Implementation
(*
* draws the button outline using highlighting and shading
*
* returns:
* True on success
*
* False on error
*)
Function ButtonBorder(
Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
Var
n, TopLColor, BotRColor, LSide,
xl, xr, yu, yd : Integer;
Begin
With ButtonInfo do
Begin
If RadioButton[ThisButton].State = Up then
Begin
TopLColor := HighlightEdge;
BotRColor := ShadowEdge;
End
Else
Begin
TopLColor := ShadowEdge;
BotRColor := HighLightEdge;
End;
LSide := SideLength;
xl := RadioButton[ThisButton].UpperLeftX;
xr := xl + LSide;
yu := RadioButton[ThisButton].UpperLeftY;
yd := yu + LSide;
SetColor(TopLColor);
For n := 0 to BorderWidth - 1 do
Begin
Line(xl+n, yd-n, xl+n, yu+n);
Line(xl+n, yu+n, xr-n, yu+n);
End;
SetColor(BotRColor);
For n := 0 to BorderWidth - 1 do
Begin
Line(xr-n, yu+n+1, xr-n, yd-n);
Line(xr-n, yd-n, xl+n+1, yd-n);
End;
End; { With ButtonInfo }
ButtonBorder := GraphResult = GrOk;
End;
(* ButtonBorder *)
(*
* adjusts button state, highlighting and shading, then calls
* ButtonBorder to make the 3D button appear to be "Up"
*
* returns:
* True on success
*
* False on error
*)
Function ButtonUp(
Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
Begin
With ButtonInfo do
Begin
RadioButton[ThisButton].State := Up;
ButtonUp := ButtonBorder(ButtonInfo, ThisButton);
End; { With ButtonInfo }
End;
(* ButtonUp *)
(*
* adjusts button state, highlighting and shading, then calls
* ButtonBorder to make the 3D button appear to be "Down"
*
* returns:
* True on success
*
* False on error
*)
Function ButtonDown(
Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
Begin
With ButtonInfo do
Begin
RadioButton[ThisButton].State := Down;
ButtonDown := ButtonBorder(ButtonInfo, ThisButton);
End; { With ButtonInfo }
End;
(* ButtonDown *)
(*
* sets button state to "Up", then draws button and associated
* option string (ButtonTag)
*
* returns:
* True on success
*
* False on error
*)
Function DrawButton(
Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
Var
x, y, color : Integer;
Begin
If not ButtonUp(ButtonInfo, ThisButton) then
Begin
DrawButton := False;
exit;
End;
With ButtonInfo do
Begin
x := RadioButton[ThisButton].UpperLeftX + SideLength + Gap;
y := RadioButton[ThisButton].UpperLeftY + SideLength shr 2;
SetColor(RadioButton[ThisButton].TagColor);
OutTextXY(x, y, RadioButton[ThisButton].Tag);
End; { With ButtonInfo }
DrawButton := GraphResult = GrOk;
End;
(* DrawButton *)
Function DrawPanel(Var ButtonPanel : PanelDescriptor) : Boolean;
Var
HighLight, Shadow, Normal, TextHighlight, TextNormal : Integer;
xl, xr, yu, yd, k : Integer;
Begin
If MouseVisible then
ToggleMouseVisibility;
With ButtonPanel do
Begin
HighLight := ButtonStyle.HighLightEdge;
Shadow := ButtonStyle.ShadowEdge;
Normal := ButtonStyle.Normal;
TextNormal := ButtonStyle.TagNormal;
TextHighLight := ButtonStyle.TagHighLight;
xl := UpperLeftX;
xr := LowerRightX;
yu := UpperLeftY;
yd := LowerRightY;
SetColor(HighLight);
For k := 0 to ButtonStyle.BorderWidth + 2 do
Begin
Line(xl+k, yd-k, xl+k, yu+k);
Line(xl+k, yu+k, xr-k, yu+k);
End;
SetColor(Shadow);
For k := 0 to ButtonStyle.BorderWidth + 2 do
Begin
Line(xr-k, yu+k+1, xr-k, yd-k);
Line(xr-k, yd-k, xl+k+1, yd-k);
End;
SetFillStyle(SolidFill, Normal);
Bar(xl+k, yu+k, xr-k, yd-k);
For k := 1 to NumButtons do
Begin
If k = CurrentButton then
ButtonStyle.RadioButton[k].TagColor := TextHighlight
Else
ButtonStyle.RadioButton[k].TagColor := TextNormal;
If not DrawButton(ButtonStyle, k) then
Begin
DrawPanel := False;
exit;
End;
End;
End; { With ButtonPanel }
If MouseOn then
ToggleMouseVisibility;
DrawPanel := True;
End;
(* DrawPanel *)
(*
* checks mouse, if active, for "left mouse button down" state
* and if True, checks mouse position to determine if a button
* has been "clicked on"
*
* returns:
* 0 if no button "clicked on"
*
* number of button in range 1 to MaxButton "clicked on"
*)
Function ButtonClick(Var ButtonPanel : PanelDescriptor) : Integer;
Var
MX, MY, MouseButtonState : Word;
wid, len, k : Integer;
RButton : Buttons;
ThisButton : Button;
Begin
If not GetMousePosition(MX, MY, MouseButtonState) then
Begin
ButtonClick := 0;
Exit;
End;
If (MouseButtonState and 1) <> 1 then
Begin
ButtonClick := 0;
Exit;
End;
With ButtonPanel do
Begin
RButton := ButtonStyle.RadioButton;
wid := ButtonStyle.BorderWidth;
len := ButtonStyle.SideLength;
For k := 1 to NumButtons do
Begin
ThisButton := RButton[k];
With ThisButton do
Begin
If (MX >= (UpperLeftX - wid)) and
(MX <= (UpperLeftX + len + wid)) and
(MY >= (UpperLeftY - wid)) and
(MY <= (UpperLeftY + len + wid)) then
Begin
ButtonClick := k;
Exit;
End;
End; { With ThisButton }
End; { For }
End; { With ButtonPanel }
ButtonClick := 0;
End;
(* ButtonClick *)
Function ButtonPress(Var ButtonPanel : PanelDescriptor) : Integer;
Var
KeyCode, ScanCode : Byte;
KeyState, Result, Done : Boolean;
HiLite, Norm, k, choice : Integer;
OldButton, ClickedOn : Integer;
Begin
KeyCode := 0;
ScanCode := 0;
ClickedOn := 0;
Result := False;
Done := False;
choice := 0;
KeyState := key_ready(KeyCode, ScanCode);
If KeyState then
get_key(KeyCode, ScanCode);
If (not KeyState) and (MouseOn) then
Begin
ClickedOn := ButtonClick(ButtonPanel);